const { list } = require('./mock/index')
const bodyPasrse = require('body-parser')
const userList = [{
	user: '张三',
	pwd: '123'
}, {
	user: 'admin',
	pwd: 'admin'
}]
module.exports = app => {
	app.use(bodyPasrse.json())

	app.get('/api/list', (req, res) => {
		const { pageCode = 1, pageSize = 10 } = req.query;
		res.send({
			code: 200,
			data: list.slice((pageCode - 1) * pageSize, pageCode * pageSize),
			total: list.length
		})
	})

	app.post('/api/detail', (req, res) => {
		const { id } = req.body
		const item = list.find(v => v.id === id)
		res.send({
			code: 200,
			data: item
		})
	})

	app.post('/api/mess', (req, res) => {
		const { id, mess } = req.body
		const item = list.find(v => v.id === id);
		item.mess.unshift(mess)
		res.send({
			code: 200,
			mess: '添加评论成功'
		})
	})

	app.post('/api/login', (req, res) => {
		const { user, pwd } = req.body
		const currUser = userList.find(v => v.user === user && v.pwd === pwd)

		if(currUser) {
			res.send({
				code: 200,
				token: Math.random()
			})
		} else {
			res.send({
				code: 404,
			})
		}
	})
}